package schedule;


import java.util.ArrayList;
import java.util.Collection;


public class CommandHistory
{
	private ArrayList<Command> commands;
	private int currentCommand;
	private int lastCommand;
	private int maxCommand;
	
	public CommandHistory(int size)
	{
		commands = new ArrayList<Command>();
		currentCommand = -1;
		lastCommand = currentCommand;
		maxCommand = size;
	}

	public void addCommand(Command command)
	{
		if(currentCommand < maxCommand - 1)
		{
			currentCommand++;
			lastCommand = currentCommand;
			commands.add(currentCommand, command);
		}
		else
		{
			commands.remove(0);
			commands.add(currentCommand, command);
		}
	}
	
	public void undo(Collection<Event> events) 
	{
		if(currentCommand >= 0)
		{
			commands.get(currentCommand).unexecute(events);
			currentCommand--;
		}
	}

	public void redo(Collection<Event> events) 
	{
		if(currentCommand < lastCommand)
		{
			currentCommand++;
			commands.get(currentCommand).execute(events);
		}
	}
}
